<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Ordermodel extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    function insert_order($order_array)
    {
        $this->db->insert('orders', $order_array);
        $query = $this->db->query('SELECT LAST_INSERT_ID()');
        foreach ($query->result_array() as $row) {
            foreach ($row as $ret)
                return $ret;
        }
    }

    function insert_commodity($commodity_array)
    {
        $this->db->insert('commodity', $commodity_array);
    }

    //列出未入账状态订单数量
    function wait_for_pay_order_num()
    {
        $query = $this->db->query("SELECT COUNT(*) FROM orders WHERE orders.state = 0");
        foreach ($query->result_array() as $row) {
            return $row['COUNT(*)'];
        }
    }

    //列出未入账状态订单id
    function wait_for_pay_ids($limit, $offset = 0)
    {
        $this->db->select('orders.id as id', false)
            ->from('orders')
            ->where('state', 0)
            ->order_by('orders.id', 'desc')
            ->limit($limit, $offset);
        return $this->db->get()->result_array();
    }

    //列出未入账订单
    function wait_for_pay_order_list($array)
    {
        $this->db->select('orders.id as id,orderTime,orderNumber,commodityNumber,imagePath,link,colorAndSize,amount,unitPrice,money,deliveryMoney,total,concat(clientName,postcode,clientAddress,clientPhone) as clientInfo,paymentMethod,cashEntryTime', false)
            ->from('orders')
            ->join('commodity', 'commodity.orderId=orders.id', 'inner')
            ->where_in('orders.id', $array)
            ->order_by('orders.id', 'desc');
        return $this->db->get()->result_array();
    }

    //改变未入账状态
    function wait_for_pay_update($id, $date)
    {
        $temp = array('cashEntryTime' => $date, 'state' => 1);
        $this->db->where('id', $id);
        $this->db->update('orders', $temp);
    }


    //未采购，已采购状态订单总数
    function wait_for_buy_order_num()
    {
        $this->db->from('orders')
            ->where('state', 1)
            ->or_where('state', 2);
        return $this->db->count_all_results();
    }

    //未采购，已采购状态订单id
    function wait_for_buy_ids($limit, $offset = 0)
    {
        $this->db->select('orders.id as id', false)
            ->from('orders')
            ->where('state', 1)
            ->or_where('state', 2)
            ->order_by('orders.id', 'desc')
            ->limit($limit, $offset);
        return $this->db->get()->result_array();
    }

    //未采购，已采购状态订单信息
    function wait_for_buy($array)
    {
        $this->db->select("orders.id as id,orderTime,orderNumber,state,commodity.id as commodityId,commodityNumber,imagePath,link,colorAndSize,amount,procurement,procureNumber,procureTime,procureAmount,procureUnitPrice,procureFreight,procureMoney", false)
            ->from('orders')
            ->join('commodity', 'commodity.orderId=orders.id', 'inner')
            ->where_in('orders.id', $array)
            ->order_by('orders.id', 'desc');
        return $this->db->get()->result_array();
    }

    //将订单状态改为1，即未采购
    function wait_for_buy_to_state1($id)
    {
        $data = array('state' => 1);
        $this->db->where('id', $id);
        $this->db->update('orders', $data);
    }

    //将订单状态改为2，即已采购
    function wait_for_buy_to_state2($id)
    {
        $data = array('state' => 2);
        $this->db->where('id', $id);
        $this->db->update('orders', $data);
    }

    //将订单状态改为3，即已配货
    function wait_for_buy_update_confirm($id)
    {
        $temp = array('state' => 3, 'allocationTime' => date('Y-m-d H:i:s', time()));
        $this->db->where('id', $id);
        $this->db->update('orders', $temp);
    }

    //列出欲操作商品信息
    function wait_for_buy_commodity($id)
    {
        $this->db->select('id,procurement,procureNumber,procureTime,procureAmount,procureUnitPrice,procureFreight,procureMoney', false)
            ->from('commodity')
            ->where('commodity.id', $id);
        return $this->db->get()->result_array();
    }

    //对商品信息进行更新
    function wait_for_buy_commodity_update($id, $data)
    {
        $this->db->where('id', $id);
        $this->db->update('commodity', $data);
    }

    //已配货待发货订单订单数量
    function wait_for_delivery_num()
    {
        $this->db->from('orders')
            ->where('state', 3);
        return $this->db->count_all_results();
    }

    //已配货待发货订单id
    function wait_for_delivery_ids($limit, $offset)
    {
        $this->db->select("orders.id as id")
            ->from('orders')
            ->where('state', 3)
            ->order_by('orders.allocationTime', 'asc')
            ->limit($limit, $offset);
        return $this->db->get()->result_array();
    }

    //列出已配货待发货订单
    function wait_for_delivery($array)
    {
        $this->db->select("orders.id as id, orderTime, orderNumber,commodityNumber,imagePath,link,colorAndSize,amount,unitPrice,money,deliveryMoney,total,concat(clientName, postcode, clientAddress, clientPhone) as clientInfo, paymentMethod,cashEntryTime,if(expressNumber is NULL, 0, 1) as statehidden", false)
            ->from('orders')
            ->join('commodity', 'commodity.orderId=orders.id', 'inner')
            ->join('delivery', 'delivery.orderId=orders.id', 'left')
            ->where_in('orders.id', $array)
            ->order_by('orders.allocationTime', 'asc');
        return $this->db->get()->result_array();
    }

    //已配货待发货订单修改
    function wait_for_delivery_update($id)
    {
        $temp = array('state' => 4);
        $this->db->where('id', $id);
        $this->db->update('orders', $temp);
    }

    //已配货待发货订单添加信息
    function wait_for_delivery_add($id)
    {
        $this->db->select("orderTime,orderNumber,commodityNumber,imagePath,link,colorAndSize,amount,unitPrice,money,deliveryMoney,total,concat(clientName, postcode, clientAddress, clientPhone) as clientInfo,paymentMethod,cashEntryTime", false)
            ->from('orders')
            ->join('commodity', 'commodity.orderId=orders.id', 'inner')
            ->where('orders.id', $id);
        $dp_temp = $this->db->get()->result_array();

        $this->db->select('orders.id as id,orderTime,orderNumber,deliveryTime,expressNumber,expressMethod,weight,actualFreight,expressComment,clientComment', false)
            ->from('orders')
            ->join('delivery', 'delivery.orderId=orders.id', 'left')
            ->where('orders.id', $id);
        $db_temp2 = $this->db->get()->result_array();

        $data = array('detail' => $dp_temp, 'delivery' => $db_temp2);
        return $data;
    }

    //得到已经存在的配货信息
    function wait_for_delivery_alter($id)
    {
        $this->db->select("orderTime,orderNumber,commodityNumber,imagePath,link,colorAndSize,amount,unitPrice,money,deliveryMoney,total,concat(clientName, postcode, clientAddress, clientPhone) as clientInfo,paymentMethod,cashEntryTime", false)
            ->from('orders')
            ->join('commodity', 'commodity.orderId=orders.id', 'inner')
            ->where('orders.id', $id);
        $dp_temp = $this->db->get()->result_array();

        $this->db->select('orders.id as id,orderTime,orderNumber,deliveryTime,expressNumber,expressMethod,weight,actualFreight,expressComment,clientComment', false)
            ->from('orders')
            ->join('delivery', 'delivery.orderId=orders.id', 'left')
            ->where('orders.id', $id);
        $db_temp2 = $this->db->get()->result_array();

        $data = array('detail' => $dp_temp, 'delivery' => $db_temp2);
        return $data;
    }

    //已发货状态订单
    function allocated_orders($array)
    {
        $this->db->select('orders.id as id,orderTime,orderNumber,commodityNumber,imagePath,link,colorAndSize,amount,unitPrice,money,deliveryMoney,total,concat(clientName,postcode,clientAddress,clientPhone) as clientInfo,paymentMethod,cashEntryTime', false)
            ->from('orders')
            ->join('commodity', 'commodity.orderId=orders.id', 'inner')
            ->where_in('orders.id', $array)
            ->order_by('orders.id', 'desc');
        return $this->db->get()->result_array();
    }

    //已发货状态订单数量
    function order_allocation_order_num()
    {
        $this->db->from('orders')
            ->where('state', 4);
        return $this->db->count_all_results();
    }

    //已发货状态订单id
    function order_allocation_ids($limit, $offset = 0)
    {
        $this->db->select('orders.id as id', false)
            ->from('orders')
            ->where('state', 4)
            ->order_by('orders.id', 'desc')
            ->limit($limit, $offset);
        return $this->db->get()->result_array();
    }
}
